﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TVNDBLayer;
using TVNDBLayer.BLL;

public partial class upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            if (Session["UID"] != null)
            {
                if (Convert.ToInt32(Session["UID"].ToString()) > 0)
                {
                    ((MainMasterPage)Page.Master).SiteWrap = "site-wrap2";
                    ((MainMasterPage)Page.Master).IsShowFooter = false;
                }
                else
                {
                    Response.Redirect("login.aspx?ref=" + ConfigurationManager.AppSettings["host"] + "upload.aspx");
                }
            }
            else
            {
                Response.Redirect("login.aspx?ref=" + ConfigurationManager.AppSettings["host"] + "upload.aspx");
            }
            LoadDropDownList();
        }

    }

    private void LoadDropDownList()
    {
        CategoryBLL cateBLL = new CategoryBLL();
        ddlCategory.DataSource = cateBLL.GetCategories();
        ddlCategory.DataTextField = "CategoryName";
        ddlCategory.DataValueField = "CategoryID";
        ddlCategory.DataBind();

    }

    protected void btnPost_Click(object sender, EventArgs e)
    {
        int iPeople = 0;
        if (!int.TryParse(rdlPeople.SelectedValue.Trim(), out iPeople))
        {
            iPeople = 0;
        }
        if (fuPhoto.HasFiles)
        {
            ArticleBLL articleBLL = new ArticleBLL();

            List<Article> listArtices = new List<Article>();
            foreach (HttpPostedFile uploadedFile in fuPhoto.PostedFiles)
            {
                Article article = new Article();
                uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Photos/"), uploadedFile.FileName));
                article.Title = uploadedFile.FileName;
                article.ContentUrl = uploadedFile.FileName;
                listArtices.Add(article);
            }
            int categoryID;
            if (!int.TryParse(ddlCategory.SelectedValue.Trim(), out categoryID))
            {
                Response.Redirect("");
            }
            AlbumBLL albumBLL = new AlbumBLL();
            Album album = new Album();
            album.AlbumTitle = album_name.Value;
            album.AlbumDescription = album_desc.Value;
            album.People = (byte?)iPeople;
            album.IsShare = chkIsShare.Checked;
            album.CreateDate = DateTime.Now;
            album.CategoryID = categoryID;
            album.UserID = Convert.ToInt32(Session["UID"].ToString());
            if (listArtices != null && listArtices.Count > 0)
            {
                album.Poster = listArtices[0].ContentUrl;
            }

            try
            {
                albumBLL.PostAlbum(album, listArtices);
                Response.Redirect("home.aspx");
            }
            catch (Exception ex)
            {

            }

        }


    }
}